home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1 / lisp / think-c / tc-edit-send.el next >
Encoding:
Text File  |  1994-03-08  |  12.3 KB  |  331 lines  |  [TEXT/EMAC]

  1. ;;;
  2. ;;; Apple event support for the Think Project Manager
  3. ;;;
  4.  
  5. (defun tc:send-parameterless (eventType description)
  6.   (let* (event
  7.          (reply (make-string sizeof-AppleEvent 0))
  8.          transactionID
  9.          (result
  10.           (catch 'panic
  11.             (throw-err (create-think-c-apple-event "KAHL" eventType
  12.                                                    event transactionID))
  13.             (throw-err (AESend event reply (+ kAEQueueReply kAENeverInteract)
  14.                                kAENormalPriority 0 0 0))
  15.             (setq ae-history (cons (cons transactionID
  16.                                          (list (cons 'description description)
  17.                                                (cons 'handler 'do-simple-reply)))
  18.                                    ae-history))
  19.             noErr)))
  20.     (if event (AEDisposeDesc event))
  21.     result))
  22. ;
  23. ; -----------------------------------------------------------------------
  24. ; -----------------------------------------------------------------------
  25. ;
  26. ; Modified --- the editor sends this event to notify THINK that a file has been modified
  27. ;              at the specified time.  The editor should send this event when saving a
  28. ;              file and when closing a file.
  29. ;         
  30. ; Event Class:    'KAHL'
  31. ; Event ID:        'MOD '
  32. ; Parameters:
  33. ;     keyDirectObject (AEDescList of the following structs (typeChar), one for each file)
  34. ;         struct {
  35. ;             FSSpec     fss;    //    the file spec
  36. ;             long    when;    //    the modified time
  37. ;             short    saved;    //    is the file being saved?  0 == no.
  38. ;         };
  39.  
  40. ; -----------------------------------------------------------------------
  41. ; -----------------------------------------------------------------------
  42. ;
  43. ; Find In Next File --- the editor sends this event to prompt THINK to execute its
  44. ;                       Find In Next File command.  This provides better THINK/Editor
  45. ;                       integration during multi-file searches.
  46. ;         
  47. ; Event Class:    'KAHL'
  48. ; Event ID:        'FINF'
  49. ; Parameters:        none
  50.  
  51. (defun tc:send-finf ()
  52.   (tc:send-parameterless "FINF" "find-in-next-file"))
  53.  
  54. ; -----------------------------------------------------------------------
  55. ; -----------------------------------------------------------------------
  56. ;
  57. ; Goto Next Match ---  the editor sends this event to prompt THINK to execute its
  58. ;                      Go To Next Match/Error command.  This provides better THINK/Editor
  59. ;                      integration during batch compiles and batch searches.
  60. ;         
  61. ; Event Class:    'KAHL'
  62. ; Event ID:        'NMAT'
  63. ; Parameters:        none
  64.  
  65. (defun tc:send-nmat ()
  66.   (tc:send-parameterless "NMAT" "goto-next-match"))
  67.  
  68. ; -----------------------------------------------------------------------
  69. ; -----------------------------------------------------------------------
  70. ;
  71. ; Goto Previous Match ---  the editor sends this event to prompt THINK to execute its
  72. ;                           Go To Previous Match/Error command.  This provides better 
  73. ;                           THINK/Editor integration during batch compiles and batch
  74. ;                           searches.
  75. ;         
  76. ; Event Class:    'KAHL'
  77. ; Event ID:        'PMAT'
  78. ; Parameters:        none
  79.         
  80. (defun tc:send-pmat ()
  81.   (tc:send-parameterless "PMAT" "goto-previous-match"))
  82.  
  83. ; -----------------------------------------------------------------------
  84. ; -----------------------------------------------------------------------
  85. ;
  86. ; Get Markers --- the editor sends this event to ask THINK for a file's marker and
  87. ;                 Debugger state data.  The editor should send this event whenever
  88. ;                 it opens a file.
  89. ;         
  90. ; Event Class:    'KAHL'
  91. ; Event ID:        'GTMK'
  92. ; Parameters:
  93. ;     keyDirectObject (AEDescList of FSSpecs (typeFSS), one for each file)
  94. ;         Indicates the file(s) just opened.
  95. ;         
  96. ;     'CLBK' (typeChar, required)
  97. ;         Specifies callback functions to be used by THINK in getting the marker data.
  98. ;         
  99. ;         struct {
  100. ;             long GetNumLines;    //    Pointer to a function that returns the number of
  101. ;                                 //    lines in a file.
  102. ;             long GetCharPos;    //    Pointer to a function that returns the character
  103. ;                                 //    position for the first character in a given line
  104. ;                                 //     of a file.
  105. ;             long refcon;        //    A 32-bit data element that THINK will pass back as
  106. ;                                 //    the first parameter of the GetNumLines and GetCharPos
  107. ;                                 //    functions.                    
  108. ;         };    
  109. ;  
  110. ; Reply Parameters:
  111. ;     keyDirectObject (AEDescList of the following structs (typeChar), one for each file)
  112. ;     
  113. ;         struct {
  114. ;             Handle     markers, breakpoints, dataviews, lineOffsets;
  115. ;             long **bkptIDs, **dtvuIDs, **bkptSizes, **dtvuSizes;
  116. ;         };
  117.  
  118. (defun tc:send-gtmk ()
  119.   (let* (event
  120.          (reply (make-string sizeof-AppleEvent 0))
  121.          transactionID
  122.          spec
  123.          (clbk-data (make-string 12 0))
  124.          (file-name (buffer-file-name))
  125.          (result
  126.           (catch 'panic
  127.             (throw-err (create-think-c-apple-event "KAHL" "GTMK"
  128.                                                    event transactionID))
  129.             (if (null file-name) (throw-err errAEEventNotHandled))
  130.             (throw-err (unix-filename-to-FSSpec file-name spec))
  131.             (throw-err (AEPutParamPtr event keyDirectObject typeFSS
  132.                                       spec (length spec)))
  133.             (encode-internal clbk-data 0 'unsigned-long tc:GetNumLines)
  134.             (encode-internal clbk-data 4 'unsigned-long tc:GetCharPos)
  135.             (encode-internal clbk-data 8 'verbatim-long (current-buffer))
  136.             (throw-err (AEPutParamPtr event "CLBK" typeChar
  137.                                       clbk-data (length clbk-data)))
  138.             (throw-err (AESend event reply (+ kAEQueueReply kAENeverInteract)
  139.                                kAENormalPriority 0 0 0))
  140.             (setq ae-history (cons (cons transactionID
  141.                                          (list (cons 'description "get-markers")
  142.                                                (cons 'handler 'tc:do-gtmk-reply)
  143.                                                (cons 'buffer (current-buffer))))
  144.                                    ae-history))
  145.             noErr)))
  146.     (if event (AEDisposeDesc event))
  147.     result))
  148.  
  149. (defun tc:try-sending-gtmk ()
  150.   ;;; This will avoid settting tc:have-TPM-data if there is none at all to be had.
  151. ;  (if (tc:file-in-project-p (file-name-nondirectory
  152. ;                             (buffer-file-name (current-buffer)))))
  153.       (tc:send-gtmk))
  154.  
  155. ;;; "Whenever we open a file..."
  156. (if (not (memq (function tc:send-gtmk) find-file-hooks))
  157.     (setq find-file-hooks (cons (function tc:try-sending-gtmk) find-file-hooks)))
  158.  
  159. (defun tc:do-gtmk-reply (event history)
  160.   (let* ((theList (make-string sizeof-AEDesc 0))
  161.          (theCount-string (make-string sizeof-long 0))
  162.          theCount-integer
  163.          (theAEKeyword (make-string sizeof-long 0))
  164.          (typeCode (make-string sizeof-long 0))
  165.          (dataPtr (make-string (* 8 sizeof-long) 0))
  166.          (actualSize (make-string sizeof-long 0))
  167.          (result
  168.           (catch 'panic
  169.             (throw-err (AEGetParamDesc event keyDirectObject typeAEList theList))
  170.             (throw-err (AECountItems theList theCount-string))
  171.             (setq theCount-integer (extract-internal theCount-string 0 'long))
  172.             (if (not (= theCount-integer 1))
  173.                 errAEEventNotHandled
  174.               (throw-err (AEGetNthPtr theList 1 typeChar theAEKeyword
  175.                                       typeCode dataPtr (* 8 sizeof-int) actualSize))
  176.               (let ((buffer (cdr (assoc 'buffer history))))
  177.                 (set-buffer buffer)
  178.                 (setf-tc:markers (extract-internal dataPtr 0 'unsigned-long))
  179.                 (setf-tc:breakpoints (extract-internal dataPtr 4 'unsigned-long))
  180.                 (setf-tc:dataviews (extract-internal dataPtr 8 'unsigned-long))
  181.                 (setf-tc:lineOffsets (extract-internal dataPtr 12 'unsigned-long))
  182.                 (setf-tc:bkptIDs (extract-internal dataPtr 16 'unsigned-long))
  183.                 (setf-tc:dtvuIDs (extract-internal dataPtr 20 'unsigned-long))
  184.                 (setf-tc:bkptSizes (extract-internal dataPtr 24 'unsigned-long))
  185.                 (setf-tc:dtvuSizes (extract-internal dataPtr 28 'unsigned-long))
  186.                 (setq tc:oldSelStart (tc:selStart))
  187.                 (setq tc:oldSelEnd (tc:selEnd))
  188.                 (setq tc:oldTextLength (tc:textLen))
  189.                 (setq tc:oldNumLines (tc:numLines))
  190.                 (setq tc:oldLineStart (tc:lineStart))
  191.                 (setq tc:oldLineEnd (tc:lineEnd))
  192.                 (setq tc:have-TPM-data t))))
  193.           noErr))
  194.     (if theList (AEDisposeDesc theList))
  195.     result))
  196.         
  197. ; -----------------------------------------------------------------------
  198. ; -----------------------------------------------------------------------
  199. ;
  200. ; Put Markers --- the editor sends this event when it wants THINK to store marker and
  201. ;                 Debugger state data into a disk file.  The editor should send this
  202. ;                 event whenever it saves a file.
  203. ; Event Class:    'KAHL'
  204. ; Event ID:        'PTMK'
  205. ; Parameters:
  206. ;     keyDirectObject (AEDescList of the following structs (typeChar), one for each file)
  207. ;     
  208. ;         struct {
  209. ;             short     copyfile,     //    Are we making a copy of the file to a disk file of
  210. ;                                 //    a different name (like THINK's "Save A Copy As…")?
  211. ;                     newfile;    //    Are we creating a new disk file (vs. saving to an
  212. ;                                 //    old one?)
  213. ;             FSSpec     fss;        //    The file spec.
  214. ;             Handle     markers, breakpoints, dataviews, lineOffsets;    //    The marker and
  215. ;             long     **bkptIDs, **dtvuIDs, **bkptSizes, **dtvuSizes;    //    Debugger data.
  216. ;         };
  217.  
  218. (c:defstruct tc:struct-ptmk ((short copyfile)
  219.                              (short newfile)
  220.                              (FSSpec fss)
  221.                              (long markers)
  222.                              (long breakpoints)
  223.                              (long dataviews)
  224.                              (long lineOffsets)
  225.                              (long bkptIDs)
  226.                              (long dtvuIDs)
  227.                              (long bkptSizes)
  228.                              (long dtvuSizes)))
  229.  
  230. (defun tc:send-ptmk (copyfile newfile spec)
  231.   (let ((obj (make-string (c:sizeof 'tc:struct-ptmk) 0)))
  232.     (c:slotset 'tc:struct-ptmk obj 'copyfile copyfile)
  233.     (c:slotset 'tc:struct-ptmk obj 'newfile newfile)
  234.     (c:slotset 'tc:struct-ptmk obj 'fss spec)
  235.     (c:slotset 'tc:struct-ptmk obj 'markers (tc:markers))
  236.     (c:slotset 'tc:struct-ptmk obj 'breakpoints (tc:breakpoints))
  237.     (c:slotset 'tc:struct-ptmk obj 'dataviews (tc:dataviews))
  238.     (c:slotset 'tc:struct-ptmk obj 'lineOffsets (tc:lineOffsets))
  239.     (c:slotset 'tc:struct-ptmk obj 'bkptIDs (tc:bkptIDs))
  240.     (c:slotset 'tc:struct-ptmk obj 'dtvuIDs (tc:dtvuIDs))
  241.     (c:slotset 'tc:struct-ptmk obj 'bkptSizes (tc:bkptSizes))
  242.     (c:slotset 'tc:struct-ptmk obj 'dtvuSizes (tc:dtvuSizes))
  243.     (let (event
  244.           (reply (make-string sizeof-AppleEvent 0))
  245.           transactionID
  246.           (result
  247.            (catch 'panic
  248.              (throw-err (create-think-c-apple-event "KAHL" "PTMK"
  249.                                                     event transactionID))
  250.              (throw-err (AEPutParamPtr event keyDirectObject typeChar
  251.                                        obj (length obj)))
  252.              (throw-err (AESend event reply (+ kAEQueueReply kAENeverInteract)
  253.                                 kAENormalPriority 0 0 0))
  254.              (setq ae-history (cons (cons transactionID
  255.                                           (list (cons 'description "put-markers")
  256.                                                 (cons 'handler 'do-simple-reply)))
  257.                                     ae-history))
  258.              noErr)))
  259.       (if event (AEDisposeDesc event))
  260.       result)))
  261.  
  262. ; -----------------------------------------------------------------------
  263. ; -----------------------------------------------------------------------
  264. ;
  265. ; Update Markers --- the editor sends this event when it needs THINK to update a file's
  266. ;                    marker and Debugger state information (this involves changing the
  267. ;                    marker locations to reflect the addition or deletion of characters
  268. ;                    at a given location in the file).  For some guidelines on when to
  269. ;                    send this event, refer to the MiniEdit source code.
  270. ;         
  271. ; Event Class:    'KAHL'
  272. ; Event ID:        'MKUP'
  273. ; Parameters:
  274. ;     keyDirectObject (AEDescList of the following structs (typeChar), one for each file)
  275. ;         struct {
  276. ;             Handle     markers, breakpoints, dataviews, lineOffsets, dtvuIDs;
  277. ;             long    oldSelStart, oldSelEnd, oldTextLength, 
  278. ;                     oldLineStart, oldLineEnd, oldNumLines,
  279. ;                     newTextLength, newNumLines, newSelEnd;
  280. ;             long    refcon;        //    A 32-bit data element that THINK will pass back as
  281. ;                                 //    the first parameter of the GetLineNum function.
  282. ;         };
  283. ;         
  284. ;     'CLBK' (typeChar, required)
  285. ;         Pointer to a callback function to be used by THINK in getting the marker data.
  286. ;         The function, GetLineNum, returns the line which contains a given
  287. ;         character position.
  288.  
  289. (defun tc:send-mkup (buffer)
  290.   (let ((mkup (make-mkup buffer)))
  291.     (if mkup
  292.         (let* (event
  293.                transactionID
  294.                (reply (make-string sizeof-AppleEvent 0))
  295.                (callback-data (encode-long-integer tc:GetLineNum))
  296.                (result
  297.                 (catch 'panic
  298.                   (throw-err (create-think-c-apple-event "KAHL" "MKUP" event transactionID))
  299.                   (throw-err (AEPutParamPtr event keyDirectObject typeChar mkup (length mkup)))
  300.                   (throw-err (AEPutParamPtr event "CLBK" typeChar callback-data sizeof-long))
  301.                   (throw-err (AESend event reply (+ kAEQueueReply kAENeverInteract)
  302.                                      kAENormalPriority 0 0 0))
  303.                   (setq ae-history (cons (cons transactionID
  304.                                                (list (cons 'description "marker-update")
  305.                                                      (cons 'handler 'do-simple-reply)))
  306.                                          ae-history))
  307.                   noErr)))
  308.           (if event (AEDisposeDesc event))
  309.           result)))) 
  310.  
  311. ; -----------------------------------------------------------------------
  312. ; -----------------------------------------------------------------------
  313. ;
  314. ; CloseProject --- the editor can send this event in response to a CloseProject event 
  315. ;                  from THINK.  Sending this event tells THINK to complete its pending  
  316. ;                  Close Project command.
  317. ;     
  318. ; Event Class:    'KAHL'
  319. ; Event ID:        'CPRJ'
  320. ; Parameters:        none
  321.  
  322. (defun tc:send-cprj ()
  323.   (tc:send-parameterless "CPRJ" "close-project"))
  324.